const axios = require('axios')
const { getTxsMetadata, assetsAddresses, getAssets } = require('../helper/chain/cardano/blockfrost')


// Modified /helper/chain/cardano/blockfrost getAddressUTXOs to limit to 1 page of UTXOs
const axiosObj = axios.create({
    baseURL: 'https://cardano-mainnet.blockfrost.io/api/v0',
    headers: {
        'project_id': 'mai' + 'nnetcxT8VaeCgVMzMTSe' + 'zZijWlVkyh6XytpS',
        'Content-Type': 'application/json'
    },
    timeout: 300000,
})

async function getAddressesUTXOs(address) {
    const utxos = []
    let page = 1
    let response
    response = await axiosObj.get(`addresses/${address}/utxos?page=1`)
    response = response.data
    utxos.push(...response)
    return utxos
}

const FACTtokenAssetID = "a3931691f5c4e65d01c429e473d0dd24c51afdb6daf88e632a6c1e516f7263666178746f6b656e";

// 100 Orcfax Validator licenses
const validatorLicenseAssetIDs = [
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303031", // Validator License #001
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303032", // Validator License #002
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303033", // Validator License #003
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303034", // Validator License #004
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303035", // Validator License #005
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303036", // Validator License #006
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303037", // Validator License #007
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303038", // Validator License #008
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303039", // Validator License #009
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303130", // Validator License #010
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303131", // Validator License #011
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303132", // Validator License #012
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303133", // Validator License #013
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303134", // Validator License #014
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303135", // Validator License #015
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303136", // Validator License #016
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303137", // Validator License #017
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303138", // Validator License #018
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303139", // Validator License #019
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303230", // Validator License #020
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303231", // Validator License #021
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303232", // Validator License #022
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303233", // Validator License #023
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303234", // Validator License #024
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303235", // Validator License #025
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303236", // Validator License #026
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303237", // Validator License #027
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303238", // Validator License #028
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303239", // Validator License #029
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303330", // Validator License #030
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303331", // Validator License #031
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303332", // Validator License #032
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303333", // Validator License #033
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303334", // Validator License #034
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303335", // Validator License #035
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303336", // Validator License #036
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303337", // Validator License #037
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303338", // Validator License #038
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303339", // Validator License #039
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303430", // Validator License #040
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303431", // Validator License #041
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303432", // Validator License #042
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303433", // Validator License #043
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303434", // Validator License #044
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303435", // Validator License #045
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303436", // Validator License #046
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303437", // Validator License #047
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303438", // Validator License #048
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303439", // Validator License #049
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303530", // Validator License #050
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303531", // Validator License #051
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303532", // Validator License #052
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303533", // Validator License #053
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303534", // Validator License #054
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303535", // Validator License #055
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303536", // Validator License #056
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303537", // Validator License #057
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303538", // Validator License #058
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303539", // Validator License #059
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303630", // Validator License #060
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303631", // Validator License #061
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303632", // Validator License #062
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303633", // Validator License #063
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303634", // Validator License #064
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303635", // Validator License #065
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303636", // Validator License #066
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303637", // Validator License #067
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303638", // Validator License #068
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303639", // Validator License #069
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303730", // Validator License #070
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303731", // Validator License #071
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303732", // Validator License #072
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303733", // Validator License #073
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303734", // Validator License #074
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303735", // Validator License #075
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303736", // Validator License #076
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303737", // Validator License #077
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303738", // Validator License #078
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303739", // Validator License #079
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303830", // Validator License #080
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303831", // Validator License #081
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303832", // Validator License #082
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303833", // Validator License #083
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303834", // Validator License #084
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303835", // Validator License #085
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303836", // Validator License #086
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303837", // Validator License #087
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303838", // Validator License #088
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303839", // Validator License #089
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303930", // Validator License #090
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303931", // Validator License #091
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303932", // Validator License #092
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303933", // Validator License #093
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303934", // Validator License #094
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303935", // Validator License #095
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303936", // Validator License #096
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303937", // Validator License #097
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303938", // Validator License #098
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023303939", // Validator License #099
    "0c6f22bfabcb055927ca3235eac387945b6017f15223d9365e6e4e43000de14056616c696461746f72204c6963656e73652023313030", // Validator License #100
];


async function getStakedStatus(assetID) {
    // Get the address holding the validator NFT
    const address = await assetsAddresses(assetID);

    // Check if wallet holds required amount of FACT (500,000 to be considered staked)
    const assets = await getAssets(address[0].address);
    const FACTamount = assets.find(item => item.unit === FACTtokenAssetID);

    if (!FACTamount || BigInt(FACTamount.quantity) < 500000000000) {
        return 0; // Either no FACT tokens held or not enough FACT tokens held to be considered staked
    }

    // Get the UTXOs of the address holding the validator NFT
    const addressUTXOs = await getAddressesUTXOs(address[0].address);

    // Get the tx hash of the UTXO holding the validator NFT
    for (const UTXO of addressUTXOs) {
        for (const asset of UTXO.amount) {
            if (validatorLicenseAssetIDs.includes(asset.unit)) {
                // Get tx metadata to ensure validator NFT is aliased (staked)
                const txMetadata = await getTxsMetadata(UTXO.tx_hash)

                if (txMetadata[0]?.label === '674') {
                    return 500000;
                } else {
                    return 0;
                }
            }
        }
    }
    return 0;
}

async function getTotalValueStaked() {
    // Run all getStakedStatus calls in parallel
    const results = await Promise.all(
        validatorLicenseAssetIDs.map(async (validatorLicenseAssetID, index) => {
            return getStakedStatus(validatorLicenseAssetID);
        })
    );

    // Sum all results
    const stakedFACT = results.reduce((sum, value) => sum + value, 0);

    return stakedFACT;
}


module.exports = {
    methodology: 'TVL is calculated by adding 500,000 FACT for each wallet that posseses a Orcfax Validator License, has aliased their wallet with a transaction containing the 674 metadata label, and possesses at least 500,000 tokens.',
    timetravel: false,
    cardano: {
        tvl: () => 0,
        staking: async () => {
            const data = await getTotalValueStaked()
            return {
                orcfax: data
            }
        }
    },
};
